package com.piik.mall.coupon.service.impl;

import com.piik.common.dto.MemberPrice;
import com.piik.common.dto.SkuReductionDTO;
import com.piik.mall.coupon.entity.MemberPriceEntity;
import com.piik.mall.coupon.entity.SkuLadderEntity;
import com.piik.mall.coupon.service.MemberPriceService;
import com.piik.mall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.piik.common.utils.PageUtils;
import com.piik.common.utils.Query;

import com.piik.mall.coupon.dao.SkuFullReductionDao;
import com.piik.mall.coupon.entity.SkuFullReductionEntity;
import com.piik.mall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 保存满减,折扣,会员价信息
     *
     * @param skuReductionDTO
     */
    @Override
    public void saveSkuFullReduction(SkuReductionDTO skuReductionDTO) {
         // 5.3 保存满减,折扣,会员价 mall-sms：sms_sku_full_reduction,sms_sku_ladder,sms_member_price
        // 满减
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionDTO,skuFullReductionEntity);
        if(skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal(0)) == 1){
            this.save(skuFullReductionEntity);
        }

        // 折扣
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuReductionDTO,skuLadderEntity);
        skuLadderEntity.setAddOther(skuReductionDTO.getCountStatus());
        if(skuLadderEntity.getFullCount() > 0){
            skuLadderService.save(skuLadderEntity);
        }



        // 会员价
        List<MemberPrice> memberPrice = skuReductionDTO.getMemberPrice();
        if(memberPrice != null && memberPrice.size()>0){
            List<MemberPriceEntity> memberPriceEntities = memberPrice.stream().map(item -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(skuReductionDTO.getSkuId());
                memberPriceEntity.setMemberPrice(item.getPrice());
                memberPriceEntity.setMemberLevelId(item.getId());
                memberPriceEntity.setMemberLevelName(item.getName());
                memberPriceEntity.setAddOther(1); // 是否可叠加
                return memberPriceEntity;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(memberPriceEntities);
        }
    }

}